package com.headwind.logistics.waybill.controller;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.ExpressGrant;
import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RedisUtils;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.log.annotate.Log;
import com.headwind.logistics.waybill.dto.ExpressRecordEmpDTO;
import com.headwind.logistics.waybill.service.intl.ExpressServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 小恶魔
 * @Date 2020-07-21 20:27
 * @Version 1.0
 * @DESCRIPTION 快递单号控制层
 */
@Controller
@RequestMapping("/admin/expressSheet")
public class ExpressController {
    //快递单号业务逻辑层
    @Resource
    private ExpressServiceIntl expressServiceIntl;

    //api响应统一接口
    @Resource
    private ResultGenerator resultGenerator;

    //redis工具类
    @Resource
    private RedisUtils redisUtils;

    /**
     * 获取快递单号取号信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryExpressRecordInfo")
    @ResponseBody
    public LayuiUtil queryExpressRecordInfo(Integer page,Integer limit){

        return LayuiUtil.data(expressServiceIntl.queryExpressRecord(null,null).size()
                ,expressServiceIntl.queryExpressRecord(page,limit));
    }

    /**
     * 获取最新的快递单号
     * @return
     */
    @GetMapping("/queryNewExpressEndNum")
    @ResponseBody
    public RestResult queryNewExpressEndNum(){

        Long endNum = expressServiceIntl.queryNewExpressEndNum();

        return resultGenerator.getSuccessResult(endNum);
    }

    /**
     * 添加快递单号
     * @param expressRecordEmpDTO
     * @param session
     * @return
     */
    @PostMapping("/addExpressNum")
    @ResponseBody
    @Log("取了快递单号(面单)")
    public RestResult addExpressNum(ExpressRecordEmpDTO expressRecordEmpDTO, HttpSession session){

        //从session获取员工对象
        Employee employee = (Employee) session.getAttribute(String.valueOf(UserSessionKey.EMPLOYEE));
        expressRecordEmpDTO.setEmployeeId(employee.getEmployeeId());
        Integer count = expressServiceIntl.addExpressNum(expressRecordEmpDTO);

        if (count > 0){
            return resultGenerator.getSuccessResult();
        }
        return resultGenerator.getFailResult("生成面单失败了呢");

    }

    /**
     * 批量删除 快递单号取号表
     * @return
     */
    @PostMapping("/batchDeleteExpressFetch")
    @ResponseBody
    @Log("批量删除了快递单号取号的号端")
    public RestResult batchDeleteExpressFetch(String expressIds){

        Integer count = expressServiceIntl.batchDeleteExpressFetch(expressIds);

        return resultGenerator.getSuccessResult("成功删除了"+count+"条快递号段",null);

    }

    /**
     * 根据行政区域id 获取 具体网点营业信息
     * @param areaId
     * @return
     */
    @GetMapping("/queryNetAreaSite")
    @ResponseBody
    public RestResult queryNetAreaSiteByAreaId(Integer areaId){

        List<BaseNetSite> baseNetSites = expressServiceIntl.queryNetAreaSiteByAreaId(areaId);

        return resultGenerator.getSuccessResult(baseNetSites);

    }

    /**
     * 添加面单发放记录表
     * @param expressGrant
     * @return
     */
    @PostMapping("/addExpressGrant")
    @ResponseBody
    @Log("发放了快递单号给营业网点")
    public RestResult addExpressGrant(ExpressGrant expressGrant,HttpSession session){

        //从session获取员工信息
        Employee employee = (Employee) session.getAttribute("EMPLOYEE");
        expressGrant.setEmployeeId(employee.getEmployeeId());
        Integer count = expressServiceIntl.addExpressGrant(expressGrant);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("添加失败，重新试试？");
    }

    /**
     * 修改面单发放表状态
     * @param expressRecordId
     * @return
     */
    @PostMapping("/updateExpressGrantStart")
    @ResponseBody
    @Log("修改了面单取号数据为无效")
    public RestResult updateExppressGrantStart(Long expressRecordId){

        Integer count = expressServiceIntl.updateExpressGrantStartById(0, expressRecordId);

        if (count > 0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("失败了呢，重新试试?");
    }

    /**
     * 获取全部面单发放记录
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryExpressGrant")
    @ResponseBody
    @Log("查看了面单发放记录")
    public LayuiUtil queryAllExpressGrant(Integer page,Integer limit){

        return LayuiUtil.data(expressServiceIntl.queryAllExpressGrantOrPage(null,null).size()
                ,expressServiceIntl.queryAllExpressGrantOrPage(page, limit));
    }

    /**
     * 根据发放日期和网点名称查询面单发放记录
     * @param page
     * @param limit
     * @param startDate
     * @param endDate
     * @param siteName
     * @return
     */
    @GetMapping("/queryExpressGrantByGrantDateOrSiteName")
    @ResponseBody
    public LayuiUtil queryExpressGrantByGrantDateOrSiteName(Integer page
            ,Integer limit,String startDate,String endDate,String siteName){

        return LayuiUtil.data(expressServiceIntl.queryExpressGrantByGrantDateOrSiteName(null,null,startDate,endDate,siteName).size()
                ,expressServiceIntl.queryExpressGrantByGrantDateOrSiteName(page, limit, startDate, endDate, siteName));
    }

}
